<?php

/*
 * litchicms
 * Copyright (C) 2010-5-26 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(22);
$can_action = array('from', 'insert');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'from';
}


$RIGHT = array();
if ($BRK_USER['group_id'] == 65535) {
    $rs = brk_select("right");
    while ($row = $Db->fetch_array($rs)) {
        $RIGHT[$row['right_id']] = $row;
    }
} else {
    foreach ($BRK_GLOBAL['RIGTHS']['node'] as $k => $v) {
        if($k==-2){continue;}
        $RIGHT[$k] = array(
            'right_id' => $k,
            'right_pid' => $BRK_GLOBAL['RIGTHS']['parent'][$k],
            'right_node' => $v,
            'right_note' => $BRK_GLOBAL['RIGTHS']['note'][$k]
        );
    }
}
!class_exists('Tree') && require 'tree.class.php';
$tree = new Tree($RIGHT, 'right_id', 'right_pid', 'right_note');
unset($RIGHT);
$RIGHT_TREE = $tree->getChilds();
array_shift($RIGHT_TREE);
$RIGHT_TREE = array_flip($RIGHT_TREE);


switch ($_POST['action']) {
    case 'from':
        //下面的这段代码把我心都写痛了.....不过总算弄出来了
        foreach ($RIGHT_TREE as $k => &$v) {
            $v = array($k, $tree->getValue($k), $tree->getLayer($k, "", 1), '', 'is_end' => FALSE, 'pid', 'childnum');
            $l = count($tree->getChilds($k));
            $thisparent = $tree->getParent($k);
            $v['pid'] = $thisparent;
            $v['childnum'] = $l - 1;
            $is_parent = $tree->getChild($k) ? TRUE : FALSE;
            $this_pre = $is_parent ? "<img src='system/data/treegif/20.gif' />" : "<img src='system/data/treegif/19.gif'   />";
            $thisonclick = $is_parent ? " onclick=\"hidechilds(" . ($l - 1) . ")\" " : "";
            $is_end = $k == array_pop($tree->getChild($thisparent)) ? TRUE : FALSE;
            $is_end && $v['is_end'] = TRUE;
            $this_node = $is_parent ? ($is_end ? 17 : 12) : ($is_end ? 16 : 14);
            if (!key_exists($thisparent, $RIGHT_TREE)) {
                $v[3] = "";
            } else {
                $v[3].=$RIGHT_TREE[$thisparent][3] . ($RIGHT_TREE[$thisparent]['is_end'] ? "<img src='system/data/treegif/18.gif'/>" : "<img src='system/data/treegif/13.gif'/>");
            }
            $this_node = $v[3] . "<img src='system/data/treegif/" . $this_node . ".gif' " . $thisonclick . "/>" . $this_pre;
            $v[2] = $this_node;
        }
        break;
    case 'insert':
        //检查所提交的权限中是否含有操作人无法使用的权限
        foreach ($_POST['select_rights'] as $k => $v) {
            if (!array_key_exists($v, $BRK_GLOBAL['RIGTHS']['node'])) {
                eMsg(0, "新建组失败.你提交的数据中含有非法权限");
                $_POST['action'] = 'error';
                break 2;
            }
        }

        if (empty($_POST['group_name'])) {
            eMsg(0, "要新建的组名不可以为空");
            $_POST['action'] = 'error';
            break;
        }

        $_POST['select_rights'] = empty($_POST['select_rights']) ? array() : $_POST['select_rights'];

        foreach ($_POST['select_rights'] as $k => $v) {
            $parents = $tree->getParents($v);
            array_shift($parents);
            foreach ($parents as $kk => $vv) {
                if (!in_array($vv, $_POST['select_rights'])) {
                    eMsg(0, "不可以越级选取,根据你的选择,请检查<b>" . $tree->getValue($vv) . "</b>的权限是否选取");
                    $_POST['action'] = 'error';
                    break 3;
                }
            }
        }

        ////先排除是否存在 下面的if就是在排除
        $rs = brk_select('group', "count(*) as num", " WHERE group_name='" . $_POST['group_name'] . "'");
        if (($row = $Db->fetch_array($rs)) && $row['num'] != 0) {
            eMsg(0, "新建权限组失败&nbsp;" . $_POST['group_name'] . "&nbsp;已经存在");
            $_POST['action'] = 'error';
            break;
        }
        if (!$group_id = brk_insert('group', array('group_name' => $_POST['group_name']))) {
            eMsg(0, "新建权限组失败.");
            $_POST['action'] = 'error';
            break;
        }

        foreach ($_POST['select_rights'] as $k => $v) {
            if (!brk_insert('group_right', array('group_id' => $group_id, 'right_id' => $v), FALSE)) {
                eMsg(0, "给组&nbsp;" . $_POST['group_name'] . "&nbsp;分配权限失败.");
                $_POST['action'] = 'error';
                break 2;
            }
        }

        if (write_conf_file($group_id)) {
            eMsg(1, "创建组&nbsp;" . $_POST['group_name'] . "&nbsp;成功.");
        }

        break;
    default:
        break;
}
?>
